package com.cskaoyan.controller.wxcontroller;

import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.wxbo.WxAddressSaveBO;
import com.cskaoyan.bean.wxvo.AddressDetailVO;
import com.cskaoyan.bean.wxvo.AddressList;
import com.cskaoyan.bean.wxvo.WxBaseRespVo;
import com.cskaoyan.service.WxAddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Gan
 * @Date 2021/7/12 14:20
 * @Description:
 */
@RestController
@RequestMapping("wx/address")
public class WxAddressController {

    @Autowired
    WxAddressService wxAddressService;

    @RequestMapping("list")
    public BaseRespVo list() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        //相应的是一个vo集合
        List<AddressList> addressLists = wxAddressService.addressList(user.getId());
        return BaseRespVo.ok(addressLists);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        AddressDetailVO addressDetailVO = new AddressDetailVO();

        String address = wxAddressService.selectAddressById2(id);
        int areaId = wxAddressService.selectAreaIdById(id);
        String areaName = wxAddressService.selectAreaNameByAreaId(areaId);
        int cityId = wxAddressService.selectCityIdById(id);
        String cityName = wxAddressService.selectCityNameByCityId(cityId);
        boolean isDefault = false;
        int def = wxAddressService.selectIsDefaultById(id);
        if (def == 1) {
            isDefault = true;
        }
        String mobile = wxAddressService.selectMobileById(id);
        String name = wxAddressService.selectNameById(id);
        int provinceId = wxAddressService.selectProvinceIdById(id);
        String provinceName = wxAddressService.selectProvinceNameByProvinceId(provinceId);
        addressDetailVO.setAddress(address);
        addressDetailVO.setAreaId(areaId);
        addressDetailVO.setAreaName(areaName);
        addressDetailVO.setCityId(cityId);
        addressDetailVO.setId(id);
        addressDetailVO.setIsDefault(isDefault);
        addressDetailVO.setMobile(mobile);
        addressDetailVO.setCityName(cityName);
        addressDetailVO.setName(name);
        addressDetailVO.setProvinceId(provinceId);
        addressDetailVO.setProvinceName(provinceName);

        return BaseRespVo.ok(addressDetailVO);
    }

    @PostMapping("delete")
    public WxBaseRespVo delete(@RequestBody AddressDetailVO addressDetailVO) {
        wxAddressService.setDeleteStatus(addressDetailVO.getId());
        return WxBaseRespVo.ok();

    }

    @PostMapping("save")
    public BaseRespVo save(@RequestBody WxAddressSaveBO wxAddressSaveBO) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();

        Integer id = wxAddressService.saveAddress(wxAddressSaveBO, user.getId());
        /* //若请求参数id是0
        int id = 0;
        //address=带车位吃xqw, areaId=1278, cityId=128,
        // id=9, isDefault=true, mobile=13879987567, name=风气刚, provinceId=11
        if (wxAddressSaveBO.getId() == 0) {
            //直接返回插入数据库的id
            id = wxAddressService.addAddress(wxAddressSaveBO, user.getId());
        }
        //否则只是设置默认地址，需取出数据库中的id。和设置is_Default为true
        wxAddressService.updateAddress(wxAddressSaveBO, user.getId());

        //响应的是id*/
        return BaseRespVo.ok(id);
    }
}
